(function ($) {
    $.fn.lucky = function (opt) {
        var opts = {
            row: 7, //姣忔帓鏄剧ず涓暟  蹇呴』涓哄鏁�
            col: 5, //姣忓垪鏄剧ず涓暟  蹇呴』涓哄鏁�
            depth: 6, //绾垫繁搴�
            iconW: 30, //鍥剧墖鐨勫
            iconH: 30, //鍥剧墖鐨勯珮
            iconRadius: 8, //鍥剧墖鐨勫渾瑙�
            data: personArray, //鍥剧墖鐨勫湴鍧€鏁版嵁
        }
        var _self = $(this);
        var settings = $.extend({}, opts, opt);
        var M = {
            WinHeight: $(this).height(),
            winWidth: $(this).width(),
            isThisTag: false,
            centerX: Math.ceil(settings.row / 2) - 1,
            centerY: Math.ceil(settings.col / 2) - 1,
            timer: null,
            isStop: false,
        };

        // 鍒濆鍖栧簲鐢�
        var initFun = function () {
            initEleFun();
            stepFun();
        };
        var initEleFun = function () {
            for (var i = 0; i < settings.depth; i++) {
                createEleFun(i);
            }
        }

        var createEleFun = function (n) {

            // 鍒涘缓鎵€鏈夌殑dom鍏冪礌
            var eleStr = '';
            for (var i = 0; i < settings.row; i++) {
                for (var r = 0; r < settings.col; r++) {
                    if (i == M.centerX && r == M.centerY) {
                        if (!M.isThisTag) {

                            eleStr += '<div style="' + styleFun(i, r) + '" class="js_current_dom"></div>';
                            M.isThisTag = true;

                        }

                    } else {
                        eleStr += '<div data-depth="' + n + '" style="' + styleFun(i, r) + '" class="element"></div>';
                    }
                }
            }
            _self.append(eleStr);
        }

        //璁剧疆姣忎釜澶村儚鐨勪綅缃笌鏍峰紡
        var styleFun = function (i, r) {
            onlyWidth = M.winWidth / settings.row,
                onlyHeight = M.WinHeight / settings.col,
                onlyCenterW = (M.winWidth / settings.row - settings.iconW) / 2,
                onlyCenterH = (M.WinHeight / settings.col - settings.iconH) / 2;
            var style = 'position:absolute;width:' + settings.iconW + 'px;height:' + settings.iconH + 'px;border-radius:' + settings.iconRadius + 'px;left:' + (i * onlyWidth + onlyCenterW) + 'px;top :' + (r * onlyHeight + onlyCenterH) + 'px;';
            return style;
        }

        //璁╂瘡涓ご鍍忚繍鍔�
        var stepFun = function () {

            var index = 0, elements = $('.element').length;
            console.log($('.element').length)
            if ($('.element').length > 200) {
            	for (var ele=0;ele<($('.element').length-150);ele++) {
            		$($('.element')[ele]).remove()
            	}
            	elements = $('.element').length
            }
            console.log($('.element').length)
            for (var i = 0; i < elements; i++) {
                var element = $('.element')[i];
                if (!!settings.data[i]) {
                    /*$(element).css('background-image', 'url(' + settings.data[i].image + ')');*/
                    $(element).html(settings.data[i].name).css('background-color','rgba(0,0,0,0)').css('width','80px')
                } else {
                    index >= (settings.data.length - 1) ? index = 1 : ++index;
                    if (settings.data.length == 1) {
                    	index = 0
                    }
                    if (settings.data.length == 0) {
                    	alert('所有人都已抽取完毕')
                    	location.reload();
                    }
                    $(element).html(settings.data[index].name).css('background-color','rgba(0,0,0,0)').css('width','80px')
                    /*$(element).css('background-image', 'url(' + settings.data[index].image + ')')
                    			.css('border','1px solid red')*/
                }

                var depth = $(element).attr('data-depth');
                Transform(element);
                element.translateZ = -depth * 200;

                setTimeout(function (element) {
                    var random = Math.floor(Math.random() * 3) + 15;
                    tick(function () {
                        if (!M.isStop) {
                            element.translateZ >= 200 ? element.translateZ = -depth * 200 : element.translateZ += random;
                        }
                    })
                }(element), 200)
            }

            //涓棿鐨勫ご鍍忚繍鍔�
            var thisElement = $('.js_current_dom')[0];
            Transform(thisElement);
            tick(function () {
                thisElement.translateZ >= 200 ? thisElement.translateZ = 0 : thisElement.translateZ += 15;
            })
        }

        var preloadImg = function(arr){

        }

        //璁╀腑闂寸殑澶村儚闅忔満鍒囨崲鑳屾櫙鍥�
        var randomFun = function () {

            M.timer = setInterval(function () {
                //濡傛灉鍐呭畾鍙风爜涓嶅瓨鍦紝鍒欎负闅忔満鍙风爜
                var randomNum = Math.floor(Math.random() * settings.data.length); //锛堥殢鏈烘暟锛�

                /*$('.js_current_dom').css({
                   'background-image': 'url(' + settings.data[randomNum].image + ')'
                })*/
                $('.js_current_dom').html(settings.data[randomNum].name).css('background-color','rgba(0,0,0,0)').css('width','80px')
            }, 50)
        }
        //鍋滄杩愬姩
        M.stop = function () {
            clearInterval(M.timer);
            M.isStop = true;
        }
        //寮€濮嬭繍鍔�
        M.open = function () {
            randomFun();
            M.isStop = false;
        }
        initFun();
        return M;
    }

})(jQuery);